package com.design.fitment.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.design.fitment.R;
import com.design.fitment.app.MyApplication;
import com.design.fitment.entity.QuestionsanswersListEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class QuestionsanswersListAdapter extends BaseAdapter {

    private List<QuestionsanswersListEntity.DataEntity> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    int  id;
    public QuestionsanswersListAdapter(Context context,List<QuestionsanswersListEntity.DataEntity> objects) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.objects=objects;
        imageLoader = MyApplication.getApp().getImageLoader();
        options = MyApplication.getApp().getImageOptions();
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public QuestionsanswersListEntity.DataEntity getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return objects.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.questionsanswers_list, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.headlineTv = (TextView) convertView.findViewById(R.id.headline_tv);
            viewHolder.timeTv = (TextView) convertView.findViewById(R.id.time_tv);
            viewHolder.timeIc = (ImageView) convertView.findViewById(R.id.time_ic);
            viewHolder.icPinlun = (ImageView) convertView.findViewById(R.id.ic_pinlun);
            viewHolder.commentTv = (TextView) convertView.findViewById(R.id.comment_tv);
            viewHolder.hitsIc = (ImageView) convertView.findViewById(R.id.hits_ic);
            viewHolder.hitsTv = (TextView) convertView.findViewById(R.id.hits_tv);
            viewHolder.resourceIc = (ImageView) convertView.findViewById(R.id.resource_ic);

            convertView.setTag(viewHolder);
        }
        initializeViews((QuestionsanswersListEntity.DataEntity)getItem(position), (ViewHolder) convertView.getTag());

        return convertView;
    }

    private void initializeViews(QuestionsanswersListEntity.DataEntity object, ViewHolder holder) {
        holder.timeTv.setText(object.getCreateTime());
        holder.headlineTv.setText(object.getTitle());
        holder.commentTv.setText(object.getComments() + "");
        holder.hitsTv.setText(object.getHits() + "");
        String imageUrl = object.getImgUrl();
             id=object.getId();
        imageLoader.displayImage(imageUrl, holder.resourceIc, options);
    }

    protected class ViewHolder {
        private TextView headlineTv;
        private TextView timeTv;
        private ImageView timeIc;
        private ImageView icPinlun;
        private TextView commentTv;
        private ImageView hitsIc;
        private TextView hitsTv;
        private ImageView resourceIc;
    }
}

